//
//  BHMeUserInfoModel.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/20.
//

import UIKit

class BHMeUserInfoModel {
    static let shared: BHMeUserInfoModel = BHMeUserInfoModel.init()
    
    var token: String = ""
    
    
    var userId: String = ""
    var username: String = "" // 账号
    var realname: String = "" // 姓名
    var avatar: String = ""
    var userSig: String = ""
    var phone: String = ""
    var companyName: String = "" // 公司名称
    var occupation: String = "" // 职业
    var sex: Int = 0  // 1男 2女
    
    var sexName: String  {
        get {
            if sex == 1 {
                return "男"
            }else if sex == 2 {
                return "女"
            }else {
                return "未知"
            }
        }
    }
    
    
    // 记录数据
    var attentionNum: Int = 0 // 我的/他的关注个数
    var fansNum: Int = 0 // 关注我的/他的(我的粉丝)
    var myLike: Int = 0  // 我的点赞(圈子)
    var totalNum: Int = 0  // 我的/他的获赞与收藏   个人主页展示使用 不可点击跳转

    func refreshUserInfo(_ dic: [String: Any]) {
        let resultDic = BHJsonTool.decodeDicFromDic(dic: dic, key: "result")
        
        let userDic = BHJsonTool.decodeDicFromDic(dic: resultDic, key: "userInfo")
        
        self.token = BHJsonTool.decodeStringFromDic(dic: resultDic, key: "token")
        
        self.username = BHJsonTool.decodeStringFromDic(dic: userDic, key: "username")
        self.userSig = BHJsonTool.decodeStringFromDic(dic: userDic, key: "userSig")
        self.realname = BHJsonTool.decodeStringFromDic(dic: userDic, key: "realname")
        self.avatar = BHJsonTool.decodeStringFromDic(dic: userDic, key: "avatar")
        self.phone = BHJsonTool.decodeStringFromDic(dic: userDic, key: "phone")
        self.sex = BHJsonTool.decodeIntFromDic(dic: userDic, key: "sex")
        self.userId = BHJsonTool.decodeStringFromDic(dic: userDic, key: "id")

        
        BHCache.save_userToken(token: self.token)
        BHCache.save_userSign(userSign: self.userSig)
    }
    
    
    
    func appendInfo(model: BHUserPageDataModel) {
        self.username = model.name
        self.sex = model.sex
        self.avatar = model.avatar
        self.companyName = model.companyName
        self.occupation = model.occupation
        self.userId = model.userId
        self.attentionNum = model.attentionNum
        self.fansNum = model.fansNum
        self.myLike = model.myLike
        self.totalNum = model.totalNum
        
        let sign = BHCache.load_userSign()
        self.userSig = sign
    }
    
    
    func clearUser()  {
        BHCache.remove_token()
        self.userId = ""
        self.username = ""
        self.avatar = ""
        self.companyName = ""
        self.occupation = ""
        self.realname = ""
        self.userSig = ""
        self.phone = ""
        self.token = ""
        self.userSig = ""
        
        self.sex = 0
        self.attentionNum =  0
        self.fansNum = 0
        self.myLike = 0
        self.totalNum = 0
        V2TIMManager.sharedInstance()?.logout({
            
        }, fail: { (code, errorMsg) in
            
        })
    }
}
